// File Name: ws2812b_task.c
// Author: kukukukuku~
// Date: 2024-06-26

#include "./User/Fan/inc/fan_ctrl.h"
#include "./User/WS2812B/inc/ws2812b_task.h"
#include "stdlib.h"
#include "string.h"

TaskHandle_t taskHandleWs2812b; //WS2812B任务句柄

/**
 * @brief   WS2812B任务
 * @param   arg:    任务参数
 * @retval  None
 * @note    None
*/
void task_ws2812b(void *arg)
{
    static uint32_t randomSrand;
    static uint8_t ws2812bIndex;
    static uint8_t ws2812bFlowModeCw;
    static uint8_t ws2812bFlowModeCcw;
    
    while(1)
    {
        if(ws2812bState == 0)
        {
            ws2812bIndex = 0;
            memset(ws2812b, 0, sizeof(ws2812b));
        }
        else if(ws2812bState == 1)
        {
            if(ws2812bFlowModeCw == 0)
            {
                if(ws2812bCwMode == 0)
                {
                    srand(randomSrand--);
                    ws2812b[ws2812bIndex].ws2812b.rData = rand() % (rand() % (rand() % 0x30)) + 1;
                    srand(randomSrand--);
                    ws2812b[ws2812bIndex].ws2812b.gData = rand() % (rand() % (rand() % 0x30)) + 1;
                    srand(randomSrand--);
                    ws2812b[ws2812bIndex].ws2812b.bData = rand() % (rand() % (rand() % 0x30)) + 1;
                }
                else
                {
                    ws2812b[ws2812bIndex].ws2812b.rData = ws2812bCwRed;
                    ws2812b[ws2812bIndex].ws2812b.gData = ws2812bCwGreen;
                    ws2812b[ws2812bIndex].ws2812b.bData = ws2812bCwBlue;
                }
            }
            else if(ws2812bFlowModeCw == 1)
            {
                ws2812b[ws2812bIndex].ws2812bRgb = 0;
            }
        }
        else if(ws2812bState == 2)
        {
            if(ws2812bFlowModeCcw == 0)
            {
                if(ws2812bCcwMode == 0)
                {
                    srand(randomSrand--);
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.rData = rand() % (rand() % (rand() % 0x30)) + 1;
                    srand(randomSrand--);
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.gData = rand() % (rand() % (rand() % 0x30)) + 1;
                    srand(randomSrand--);
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.bData = rand() % (rand() % (rand() % 0x30)) + 1;
                }
                else
                {
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.rData = ws2812bCcwRed;
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.gData = ws2812bCcwGreen;
                    ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812b.bData = ws2812bCcwBlue;
                }
            }
            else if(ws2812bFlowModeCcw == 1)
            {
                ws2812b[WS2812B_NUM - ws2812bIndex - 1].ws2812bRgb = 0;
            }
        }
        
        if(ws2812bState && ++ws2812bIndex >= WS2812B_NUM)
        {
            ws2812bIndex = 0;
            
            if(ws2812bState == 1)
            {
                ws2812bFlowModeCcw = 0;
                
                if(++ws2812bFlowModeCw >= 2)
                {
                    ws2812bFlowModeCw = 0;
                }
            }
            else if(ws2812bState == 2)
            {
                ws2812bFlowModeCw = 0;
                
                if(++ws2812bFlowModeCcw >= 2)
                {
                    ws2812bFlowModeCcw = 0;
                }
            }
        }
        
        ws2812b_send_reset();
        for(uint8_t i = 0; i < WS2812B_NUM; i++)
        {
            ws2812b_send_rgb_data(&ws2812b[i]);
        }
        
        vTaskDelay(100);
    }
}
